//
//  PickerThreeView.m
//  ATMCash
//
//  Created by zhengchengfeng on 2024/9/12.
//

#import "PickerThreeView.h"
#import "PickerTableViewCell.h"

#define kTabCell   @"kTabCell"

@interface PickerThreeView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UIView *wholeView;

@property (nonatomic) UITableView *tableView;

@property (nonatomic) NSMutableArray *orderArray,*selectArr;

@property (nonatomic) NSArray *arr;


@end

@implementation PickerThreeView

- (instancetype)initWithFrame:(CGRect)frame withTitleStr:(NSString *)titleStr withDataArr:(NSArray *)arr{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kBACK_SHADOWCOLOR;
        
        UIImage *img = [UIImage imageNamed:@"icon_idf13"];
        
        CGFloat kHeight = DimensionBaseIphone6(img.size.height) + 10 + 12 + 48 + (IS_IPhoneX_All ? kHeight_BottomSafe : 15);
        
        CGFloat tabH = arr.count * 48;
        
        if (arr.count >= 5) {
            tabH = 48 *5;
        }
        
        kHeight += tabH;
        
        self.arr = arr;
        self.selectArr = [[NSMutableArray alloc] init];
        
        self.wholeView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kHeight)];
        self.wholeView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.wholeView];
        
        
        
        UIImageView *topImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, DimensionBaseIphone6(img.size.height))];
        [self.wholeView addSubview:topImgView];
        topImgView.image = img;
        topImgView.userInteractionEnabled = YES;
        
        UILabel *titleLab = [Utility createLabel:[UIFont fontWithName:kFuturaMedium size:16] color:[UIColor colorWithHexString:@"#022D06"]];
        [topImgView addSubview:titleLab];
        titleLab.textAlignment  = 1;
        titleLab.frame = CGRectMake(0, DimensionBaseIphone6(20), kScreenWidth, DimensionBaseIphone6(20));
        titleLab.text = esString(titleStr);
        
        UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 18 - 24, topImgView.bottom - 24, 24, 24)];
        [cancelBtn setImage:[UIImage imageNamed:@"icon_idf14"] forState:UIControlStateNormal];
        [cancelBtn setImage:[UIImage imageNamed:@"icon_idf14"] forState:UIControlStateSelected];
        [cancelBtn setImage:[UIImage imageNamed:@"icon_idf14"] forState:UIControlStateHighlighted];
        [cancelBtn addTarget:self action:@selector(clickCacel) forControlEvents:UIControlEventTouchUpInside];
        [topImgView addSubview:cancelBtn];
        
        UIView *bvv = [[UIView alloc] initWithFrame:CGRectMake(0, topImgView.bottom, kScreenWidth, 10)];
        bvv.backgroundColor = [UIColor whiteColor];
        [self.wholeView addSubview:bvv];
        
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, bvv.bottom, kScreenWidth, self.wholeView.size.height - bvv.bottom)];
        backView.backgroundColor = [UIColor whiteColor];
        [self.wholeView addSubview:backView];
        
        UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, backView.frame.size.height - 48 - (IS_IPhoneX_All ? kHeight_BottomSafe : 15), kScreenWidth - 30, 48)];
        confirmBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:16];
        [confirmBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateNormal];
        [confirmBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateSelected];
        [confirmBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateHighlighted];
        [confirmBtn setTitle:@"Confirming" forState:UIControlStateNormal];
        [confirmBtn setTitle:@"Confirming" forState:UIControlStateSelected];
        [confirmBtn setTitle:@"Confirming" forState:UIControlStateHighlighted];
        [confirmBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateNormal];
        [confirmBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateSelected];
        [confirmBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateHighlighted];
        [confirmBtn addTarget:self action:@selector(clickConfirmBtn) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:confirmBtn];

        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,kScreenWidth,tabH) style:UITableViewStylePlain];
        
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.showsVerticalScrollIndicator = NO;
        [backView addSubview:self.tableView];
        
        [self.tableView registerClass:[PickerTableViewCell class] forCellReuseIdentifier:kTabCell];
        
    
        self.orderArray = [FrightensModel mj_objectArrayWithKeyValuesArray:arr];
        
    
        [self.tableView reloadData];

        
    }
    return self;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 48;
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.orderArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PickerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTabCell];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.orderArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    PickerTableViewCell *selCell = [tableView cellForRowAtIndexPath:indexPath];
    FrightensModel *mm  = selCell.model;
    mm.isShow = !mm.isShow;
    
    [self.selectArr removeAllObjects];
    [self.selectArr addObject:mm];
    
    [self.orderArray removeAllObjects];
    self.orderArray = [FrightensModel mj_objectArrayWithKeyValuesArray:self.arr];
    [self.orderArray replaceObjectAtIndex:indexPath.row withObject:mm];
    [self.tableView reloadData];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *vv = [[UIView alloc]init];
    return vv;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.00001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *vv = [[UIView alloc]init];
    return vv;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001f;
}

- (void)clickConfirmBtn{
    [self clickCacel];
    
    if (self.selectArr.count > 0) {
        if (_callBackModelArr) {
            _callBackModelArr(self.selectArr);
        }
    }
    
}

- (void)showBottomView{
    UIImage *img = [UIImage imageNamed:@"icon_idf13"];
    CGFloat kHeight = DimensionBaseIphone6(img.size.height) + 10 + 12 + 48 + (IS_IPhoneX_All ? kHeight_BottomSafe : 15);
    CGFloat tabH = self.orderArray.count * 48;
    if (self.orderArray.count >= 5) {
        tabH = 48 *5;
    }
    kHeight += tabH;
    
    kWeakSelf
    [UIView animateWithDuration:0.25 animations:^{
        
        weakSelf.wholeView.frame = CGRectMake(0, kScreenHeight - kHeight, kScreenWidth, kHeight);
         
     } completion:^(BOOL finished) {}];
    
}

- (void)clickCacel{
    UIImage *img = [UIImage imageNamed:@"icon_idf13"];
    CGFloat kHeight = DimensionBaseIphone6(img.size.height) + 10 + 12 + 48 + (IS_IPhoneX_All ? kHeight_BottomSafe : 15);
    CGFloat tabH = self.orderArray.count * 48;
    if (self.orderArray.count >= 5) {
        tabH = 48 *5;
    }
    kHeight += tabH;
    
    kWeakSelf
    [UIView animateWithDuration:0.25 animations:^{
        
         weakSelf.wholeView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kHeight);
         
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
